<?php

class Util_Where {
	
	static public function createWhereStatement($where = null,$adapter = null){
		if(!$where){
			$where = Zend_Controller_Front::getInstance()->getRequest()->getParam('where');
		};
		$operators = array('AND','OR','and','or');
		$signs = array('=','<','>','<=','>=','like','!=','IS','is','LIKE','IS NOT','is not');
		
		// przerobic rekurencyjnie
		if($where != null && $where != 'false'){// czy to nie zepsuło gdzies - nie powinno
			if($adapter == null)$adapter = Zend_Db_Table::getDefaultAdapter();
			
			if(!json_decode($where)){// SIC - to jest przy submitowaniu frame_forma (przeropbic tam zeby sie dorbze pola kodowały) - zmieniło sie kodowanie przy zmianie ext3 na 4;
				$where = json_decode(str_replace("'", '"', $where));
			} else {
				$where = json_decode($where);
			};
									
			$whereStatement = '(';
			for ($j=0;$j<count($where->parts);$j++){
				if(property_exists($where->parts[$j],'parts')){
					if(property_exists($where->parts[$j],'operator') && in_array($where->parts[$j]->operator,$operators))$whereStatement .= ' '.$where->parts[$j]->operator.' ';
					$whereStatement .= '(';
					for ($i=0;$i<count($where->parts[$j]->parts);$i++){
						$TEMP = $where->parts[$j]->parts[$i];
						
						$SIGN = (property_exists($TEMP,'sign')&& in_array($TEMP->sign, $signs))? $TEMP->sign : '=';
						$fieldPart = $adapter->quoteIdentifier($TEMP->field).' '.$SIGN.' ';
						//strpos($TEMP->field,'$')===false ? str_replace('$',$adapter->info('name'), $TEMP->field) : $TEMP->field
						if ($TEMP->value == 'NULL')$statement = $fieldPart.$TEMP->value;
						else $statement = $adapter->quoteInto($fieldPart.'?',$TEMP->value);
						
						if(property_exists($TEMP,'operator') && in_array($TEMP->operator,$operators)) $whereStatement .= ' '.$TEMP->operator.' ';
						//$whereStatement .= $operator == null ? '' : ' '.$TEMP->operator .' ';// co to jest $op ?? 
						$whereStatement.= $statement;
					}
					$whereStatement .= ')';
				} else {
					$TEMP = $where->parts[$j];
					$SIGN = (property_exists($TEMP,'sign')&& in_array($TEMP->sign, $signs))? $TEMP->sign : '=';
					$fieldPart = $adapter->quoteIdentifier($TEMP->field).' '.$SIGN.' ';
					
					if ($TEMP->value == 'NULL')$statement = $fieldPart.$TEMP->value;
					else $statement = $adapter->quoteInto($fieldPart.'?',$TEMP->value);
					
					if(property_exists($TEMP,'operator')&& in_array($TEMP->operator,$operators))$whereStatement .= ' '.$TEMP->operator.' ';
					$whereStatement.= $statement;
				}
			}
			$whereStatement .= ')';
			$where = $whereStatement;
		} else $where = null;
		//self::log($where);
		return $where;
	}
	
	/**
	 * @deprecated - zostawione dla przykładu
	 */
	static public function createJoinDefinition_BAK($join,$store,$adapter = null){
		
		function makeForJ($j,$store,$adapter=null){
			if($adapter == null)$adapter = Zend_Db_Table::getDefaultAdapter();
			$store = $adapter->quoteIdentifier($store);
			$table = $adapter->quoteIdentifier($j->table);
			if(property_exists($j,'using') && $j->using != null){
				$using = $adapter->quoteIdentifier($j->using);
				$logic = $store.'.'.$using.' = '.$table.'.'.$using; 	
			} else {
				$native = $adapter->quoteIdentifier($j->on->native);
				$host = $adapter->quoteIdentifier($j->on->host);
				$nativeStore = property_exists($j->on,'nativeStore') ? $adapter->quoteIdentifier($j->on->nativeStore) : $store;
				$logic = $nativeStore.'.'.$native.' = '.$table.'.'.$host;// sign niema ale nieważne w sumie
			}
			$fields = property_exists($j,'fields')?	$j->fields : array();
						
			return array($j->table,$logic,$fields);
		}
		
		if($join != null && $join != ''){
			$j = json_decode($join);
			if (is_array($j)){
				$jo_array = array();
				foreach($j as $jo){array_push($jo_array,makeForJ($jo,$store,$adapter));}
				return $jo_array;
			} else {
				return array(makeForJ($j,$store,$adapter));
			}
		} else return array(null,null);
	}

	static public function getValueFromWhere($param,$where=null){
		if($where==null){
			$rq = Zend_Controller_Front::getInstance()->getRequest();
			$where = $rq->getParam('where');
		}
		$where = json_decode($where);
		if(!$where){
			return $rq->getParam($param);
		} else if(property_exists($where, 'parts')){
			foreach($where->parts as $part){
				if($part->field == $param){
					return $part->value;
				}
			}
		} else if(property_exists($where, $param)){
			return $where->$param;
		}
	}
	
}
	